package com.xintonghua.bussiness.ui.user.manage;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.GoodsManageBean;
import com.xintonghua.bussiness.event.AddProductEventBus;
import com.xintonghua.bussiness.util.GlideUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wanxianyi
 * 时间：2018/3/9.
 * 包名：com.xintonghua.bussiness.ui.user.manage
 */

public class AddProductActivity extends BaseActivity {
    @BindView(R.id.edt_product_search)
    EditText edtProductSearch;
    @BindView(R.id.tv_product_add)
    TextView tvProductAdd;
    @BindView(R.id.iv_goods_head)
    ImageView ivGoodsHead;
    @BindView(R.id.tv_goods_name)
    TextView tvGoodsName;
    @BindView(R.id.tv_goods_price)
    TextView tvGoodsPrice;
    @BindView(R.id.rl_goods)
    RelativeLayout rlGoods;
    @BindView(R.id.tv_goods_count)
    TextView tvGoodsCount;
    @BindView(R.id.iv_product_reduce)
    ImageView ivProductReduce;
    @BindView(R.id.tv_product_count)
    TextView tvProductCount;
    @BindView(R.id.iv_product_add)
    ImageView ivProductAdd;
    @BindView(R.id.ll_product)
    LinearLayout ll_product;
    private int repertory;
    private int count = 1;
    private int productId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_product);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("添加产品");
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        edtProductSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    String keyword = edtProductSearch.getText().toString();
                    if (TextUtils.isEmpty(keyword)) {
                        return false;
                    }
                    MyUtils.hideSoftInput(AddProductActivity.this);
                    Bundle bundle = new Bundle();
                    bundle.putString(BuildConfig.FLAVOR, keyword);
                    openActivity(ProductListActivity.class, bundle);
                    return true;
                }
                return false;
            }
        });
    }

    @OnClick({R.id.tv_product_add, R.id.iv_product_add, R.id.iv_product_reduce})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_product_add:
                httpCent.saveStockProduct(productId, count, this, MyState.requestOne);
                break;
            case R.id.iv_product_add:
                if (count < repertory) {
                    count++;
                    tvProductCount.setText(count + "");
                } else {
                    mToast("库存不足");
                }
                break;
            case R.id.iv_product_reduce:
                if (count > 1) {
                    count--;
                    tvProductCount.setText(count + "");
                }
                break;
        }
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                EventBus.getDefault().post(new AddProductEventBus());
                mToast("添加成功！");
                finish();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(GoodsManageBean bean) {
        edtProductSearch.setText("");
        ll_product.setVisibility(View.VISIBLE);
        repertory = bean.getInventory();
        productId = bean.getId();
        GlideUtils.load(this, ivGoodsHead, bean.getImgs());
        tvGoodsName.setText(bean.getName());
        tvGoodsPrice.setText(MyUtils.getMoney(this, bean.getPrice()));
        tvGoodsCount.setText("剩余库存" + bean.getInventory() + "件");
    }
}
